/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.tms.entity.SpecialLine;
import com.jeeplus.modules.tms.mapper.SpecialLineMapper;
import com.jeeplus.modules.tms.entity.LineReceive;
import com.jeeplus.modules.tms.mapper.LineReceiveMapper;
import com.jeeplus.modules.tms.entity.LineSend;
import com.jeeplus.modules.tms.mapper.LineSendMapper;

/**
 * 专线Service
 * @author dhw
 * @version 2018-01-17
 */
@Service
@Transactional(readOnly = true)
public class SpecialLineService extends CrudService<SpecialLineMapper, SpecialLine> {

	@Autowired
	private LineReceiveMapper lineReceiveMapper;
	@Autowired
	private LineSendMapper lineSendMapper;
	
	public SpecialLine get(String id) {
		SpecialLine specialLine = super.get(id);
		specialLine.setLineReceiveList(lineReceiveMapper.findList(new LineReceive(specialLine)));
		specialLine.setLineSendList(lineSendMapper.findList(new LineSend(specialLine)));
		return specialLine;
	}
	
	public List<SpecialLine> findList(SpecialLine specialLine) {
		return super.findList(specialLine);
	}
	
	public Page<SpecialLine> findPage(Page<SpecialLine> page, SpecialLine specialLine) {
		return super.findPage(page, specialLine);
	}
	
	@Transactional(readOnly = false)
	public void save(SpecialLine specialLine) {
		super.save(specialLine);
		for (LineReceive lineReceive : specialLine.getLineReceiveList()){
			if (lineReceive.getId() == null){
				continue;
			}
			if (LineReceive.DEL_FLAG_NORMAL.equals(lineReceive.getDelFlag())){
				if (StringUtils.isBlank(lineReceive.getId())){
					lineReceive.setSpecialLine(specialLine);
					lineReceive.preInsert();
					lineReceiveMapper.insert(lineReceive);
				}else{
					lineReceive.preUpdate();
					lineReceiveMapper.update(lineReceive);
				}
			}else{
				lineReceiveMapper.delete(lineReceive);
			}
		}
		for (LineSend lineSend : specialLine.getLineSendList()){
			if (lineSend.getId() == null){
				continue;
			}
			if (LineSend.DEL_FLAG_NORMAL.equals(lineSend.getDelFlag())){
				if (StringUtils.isBlank(lineSend.getId())){
					lineSend.setSpecialLine(specialLine);
					lineSend.preInsert();
					lineSendMapper.insert(lineSend);
				}else{
					lineSend.preUpdate();
					lineSendMapper.update(lineSend);
				}
			}else{
				lineSendMapper.delete(lineSend);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(SpecialLine specialLine) {
		super.delete(specialLine);
		lineReceiveMapper.delete(new LineReceive(specialLine));
		lineSendMapper.delete(new LineSend(specialLine));
	}

	@Transactional(readOnly = false)
	public void saveReceive(LineReceive lineReceive) {
		if (lineReceive.getIsNewRecord()){
			lineReceive.preInsert();
			lineReceiveMapper.insert(lineReceive);
		}else{
			lineReceive.preUpdate();
			lineReceiveMapper.update(lineReceive);
		}
	}

	@Transactional(readOnly = false)
	public void saveSend(LineSend lineSend) {
		if (lineSend.getIsNewRecord()){
			lineSend.preInsert();
			lineSendMapper.insert(lineSend);
		}else{
			lineSend.preUpdate();
			lineSendMapper.update(lineSend);
		}
	}
	
}